﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace FlashNut
{
    public class FlashNutReader
    {
        private FlashNutter flashNutter;
        private FileInfo flashFile;

        public FlashNutReader()
        {
            this.New();
        }

        public void Open(string FlashFileName)
        {
            flashFile = new FileInfo(FlashFileName);
            flashNutter = FlashNutter.LoadFromFile(flashFile.FullName);
        }

        public void New()
        {
            flashNutter = new FlashNutter();
        }

        public void Save(string FlashFileName)
        {
            flashNutter.SaveToFile(FlashFileName);
        }

        public FlashNutter FlashNutter
        {
            get
            {
                return flashNutter;
            }
        }

        public FlashNutterDeck AddDeck(string Name)
        {
            //check if name already exists
            FlashNutterDeck deck = new FlashNutterDeck() { Name = Name };
            flashNutter.Deck.Add(deck);
            return deck;
        }

        public void DeleteDeck(FlashNutterDeck deck)
        {
            flashNutter.Deck.Remove(deck);
        }

        public void AddFlash(FlashNutterDeck deck, string Question, string Answer)
        {
            if (deck == null)
                return;

            deck.Flash.Add(new FlashNutterDeckFlash() { Question = Question, Answer = Answer });
        }

        public void DeleteFlash(FlashNutterDeck deck, FlashNutterDeckFlash flash)
        {
            deck.Flash.Remove(flash);
        }

        public FlashNutterDeckFlash NextFlash(FlashNutterDeck deck, FlashNutterDeckFlash flash)
        {
            if (flash == null)
                return null;

            FlashNutterDeckFlash NextFlash = (from FlashNutterDeckFlash f in deck.Flash
                                              where f.Current == false && f.Review == false
                                              select f).FirstOrDefault();

            if (NextFlash == null)  //no more flash
            {
                flash.Review = true;
                flash.Current = false;
            }
            else
            {
                flash.Review = true;
                flash.Current = false;
                NextFlash.Current = true;
            }

            return NextFlash;

        }

        public void ResetReview(FlashNutterDeck deck)
        {
            if (deck == null)
                return;

            foreach (FlashNutterDeckFlash f in deck.Flash)
            {
                f.Current = false;
                f.Review = false;
            }

            deck.Flash[0].Current = true;
        }

    }
}
